﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VideoRentalShop.View.Templates;
using VideoRentalShop.Controller;

namespace VideoRentalShop.View.Panel.ReturnStepPanel
{
    /// <summary>
    /// Logika interakcji dla klasy ChooseClientStepPanel.xaml
    /// </summary>
    public partial class ChooseClientStepPanel : StepPanelContent
    {
        private VideoRentalShopController _controller = VideoRentalShopController.Instance;
        private List<long> _overheldMovies;


        public ChooseClientStepPanel()
        {
            InitializeComponent();
            _overheldMovies = new List<long>();
        }


        private void RefreshCombobox()
        {
            cmbClients.Items.Clear();

            var ids = _controller.Data.OrderDetails.GetAll().Where(x => x.ReturnDate == null).Select(x => x.Order.ClientId).Distinct().ToList();
            foreach (var client in _controller.Data.Client.GetAll().Where(x => ids.Contains(x.Id)).OrderBy(x => new { x.LastName, x.Name }))
            {
                cmbClients.Items.Add(new Label { Content = string.Format("{0} {1}", client.LastName, client.Name), Tag = client.Id });
            }
        }

        private void cmbClients_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lstHeldMovies.Items.Clear();
            lstOverheldMovies.Items.Clear();

            if (cmbClients.SelectedIndex > -1)
            {
                var id = (long)(((Label)(cmbClients.SelectedItem)).Tag);

                var heldMovies = _controller.Data.OrderDetails
                    .GetAll()
                    .Where(x => x.Order.ClientId == id && x.ReturnDate == null)
                    .OrderBy(x => x.MaxReturnDate)
                    .Select(x => new { Id = x.MovieId, Title = x.Movie.Title, Date = x.Order.MakeDate, DateMaxTo = x.MaxReturnDate, OrderId = x.OrderId });

                foreach (var movie in heldMovies)
                {
                    lstHeldMovies.Items.Add(new
                    {
                        Id = movie.Id,
                        OrderId = movie.OrderId,
                        Movie = movie.Title,
                        Date = movie.Date.ToString(VideoRentalShop.Properties.Resources.StrDateFormat),
                        DateMaxTo = movie.DateMaxTo.ToString(VideoRentalShop.Properties.Resources.StrDateFormat),
                        MaxReturnDate = movie.DateMaxTo
                    });
                }

                var overheldMovies = _controller.Data.OrderDetails
                    .GetAll()
                    .Where(x =>
                        x.Order.ClientId == id && (
                            (x.ReturnDate.HasValue && x.ReturnDate.Value > x.MaxReturnDate) ||
                            (!x.ReturnDate.HasValue && x.MaxReturnDate < DateTime.Now)
                        ))
                    .OrderBy(x => new { x.Order.MakeDate, x.MaxReturnDate })
                    .Select(x => new { Id = x.MovieId, Title = x.Movie.Title, Date = x.Order.MakeDate, DateMaxTo = x.MaxReturnDate, DateTo = x.ReturnDate });

                _overheldMovies.Clear();
                foreach (var movie in overheldMovies)
                {
                    _overheldMovies.Add(movie.Id);
                    lstOverheldMovies.Items.Add(new
                    {
                        Id = movie.Id,
                        Movie = movie.Title,
                        Date = movie.Date.ToString(VideoRentalShop.Properties.Resources.StrDateFormat),
                        DateTo = movie.DateTo.HasValue ? movie.DateTo.Value.ToString(VideoRentalShop.Properties.Resources.StrDateFormat) : string.Empty,
                        DateMaxTo = movie.DateMaxTo.ToString(VideoRentalShop.Properties.Resources.StrDateFormat)
                    });
                }
            }
        }

        public override void ClearForm()
        {
            RefreshCombobox();
            cmbClients.SelectedIndex = -1;
            lstHeldMovies.Items.Clear();
            lstOverheldMovies.Items.Clear();
        }

        public override bool ValidateForm()
        {
            if (cmbClients.SelectedIndex == -1)
            {
                MessageBox.Show("Wymagane jest uprzednie wybranie klienta", "Niekompletne dane");
                return false;
            }

            return true;
        }

        public override void UpdateItem()
        {
            var id = (long)(((Label)cmbClients.SelectedItem).Tag);

            var hasOverheld = _overheldMovies.Count > 0;
            var movies = new List<MovieReturnInfo>();
            foreach (dynamic movie in lstHeldMovies.Items)
            {
                var isOverheld = hasOverheld && _overheldMovies.Contains(movie.Id);
                movies.Add(new MovieReturnInfo { MovieId = movie.Id, Title = movie.Movie, Date = movie.Date, MaxReturnDate = movie.MaxReturnDate, OverHeld = isOverheld, OrderId = movie.OrderId });
            }

            _item = new OrderReturn { ClientId = id, Movies = movies };
        }
    }
}
